package uk.co.revisual.domino.injectors
{
	import org.puremvc.as3.multicore.interfaces.IProxy;
	import org.puremvc.as3.multicore.patterns.observer.Notifier;
	
	import uk.co.revisual.creation.ClassFactory;
	import uk.co.revisual.creation.Creatable;
	import uk.co.revisual.creation.DynamicClassFactory;
	import uk.co.revisual.domino.controller.cmaps.DominoCmdMap;
	import uk.co.revisual.domino.proxies.FSMClassFactoryProxy;
	import uk.co.revisual.domino.proxies.FSMHistoryProxy;
	import uk.co.revisual.domino.proxies.FSMModelProxy;

	public class DominoInjector extends Notifier
	{
		private var _fsm:XML;
		private var _classes:Array;
		private var _factory:Creatable;
		
		public function DominoInjector( fsm:XML, classes:Array )
		{
			_fsm = fsm;
			_classes = classes;
		}
				
		override public function initializeNotifier(key:String):void{
			super.initializeNotifier( key );
			injectDomino(); // inject Domino first so recieves actions before FSM
			injectFSM();
			
		}
		
		private function injectDomino():void{
			var factory:IProxy = new FSMClassFactoryProxy( ClassFactory );
			var historyProxy:IProxy = new FSMHistoryProxy();
			var fsmProxy:FSMModelProxy = new FSMModelProxy( _fsm );
			var cmdMap:DominoCmdMap = new DominoCmdMap();
			cmdMap.initializeNotifier( multitonKey );
			cmdMap.occupy();
			
			facade.registerProxy( fsmProxy );
			facade.registerProxy( historyProxy  );	
			facade.registerProxy( factory  );	
			
			_factory = Creatable( factory );
			
			DynamicClassFactory(factory).addClass( _classes );
			
		}
		
		private function injectFSM():void{
			var injector:CustomClassFSMInjector = new CustomClassFSMInjector( _fsm, _factory );
			injector.initializeNotifier(multitonKey);
			injector.inject();
		}
		
		
		
		
	}
}